<script setup>
import { ref,onMounted } from 'vue';
import {getLevelApi} from '@/api/nursingLevel'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'



const searchForm = ref({
  page: 1,
  pageSize: 5,
  name: '',
  status: ''
})
const background = ref(false)
const disabled = ref(false)
const total=ref(0)
const currentPage = ref(1)
const pageSize = ref(5)
const tableData = ref([])


const getLevel =async()=>{
  const result = await getLevelApi(currentPage.value,pageSize.value,searchForm.value.name,searchForm.value.status)
  tableData.value = result.data.rows
  total.value = result.data.total
}

const handleSizeChange = (val) => {
  pageSize.value = val
  getLevel()
}
const handleCurrentChange = (val) => {
  currentPage.value = val
  getLevel()
}


//新增护理项目
//1.定义表单
const title = ref('')
const dialogFormVisible = ref(false)
const addForm = ref({
  name: '',
  orderNo: '',
  unit: '',
  price: '',
  image: '',
  nursingRequirement: '',
  status: '',
  remark: ''
})

const handleAvatarSuccess= (
  response,
  uploadFile
) => {
  addForm.value.image = response.data
}

const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('只支持上传jpeg图片')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('只能上传小于2M的图片')
    return false
  }
  return true
}
const addProject = async()=>{
  title.value = '添加护理项目'
  dialogFormVisible.value = true
  addForm.value={
    name: '',
    orderNo: '',
    unit: '',
    price: '',
    image: '',
    nursingRequirement: '',
    status: '',
    remark: ''
  }
}
const save = async()=>{
  let res;
  if(addForm.value.id){
    res = await updatePorjectApi(addForm.value)
    if(res.code === 1){
      ElMessage.success('修改成功')
      dialogFormVisible.value = false
      getLevel()
    }else{
      ElMessage.error('修改失败')
    }
  }else{
    res = await addPorjectApi(addForm.value)
    if(res.code === 1){
      ElMessage.success('添加成功')
      dialogFormVisible.value = false
      getLevel()
    }else{
      ElMessage.error('添加失败')
    }
  }
}
//修改护理项目
const getById=async(id)=>{
  title.value = '修改护理项目'
  dialogFormVisible.value = true
  const res = await queryInfoApi(id)
  if(res.code === 1){
    addForm.value = res.data
  }
}
//删除护理项目
const deleteById=async(id)=>{
  const res = await deletePorjectApi(id)
  if(res.code === 1){
    ElMessage.success('删除成功')
    getLevel()
  }else{
    ElMessage.error('删除失败')
  }
}


onMounted(()=>{
  getLevel()
})

</script>
<template>
    <el-dialog v-model="dialogFormVisible" :title="title" width="500">
    <el-form :model="addForm">
      <el-form-item label="名称" :label-width="formLabelWidth">
        <el-input v-model="addForm.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="排序号" :label-width="formLabelWidth">
        <el-input v-model="addForm.orderNo" autocomplete="off" />
      </el-form-item>
      <el-form-item label="单位" :label-width="formLabelWidth">
        <el-input v-model="addForm.unit" autocomplete="off" />
      </el-form-item>
      <el-form-item label="价格" :label-width="formLabelWidth">
        <el-input v-model="addForm.price" autocomplete="off" />
      </el-form-item>
      <el-form-item label="图片" :label-width="formLabelWidth">
          <el-upload
              class="avatar-uploader"
              action="api/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
  >
    <img v-if="addForm.image" :src="addForm.image" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
      </el-form-item>
      <el-form-item label="护理要求" :label-width="formLabelWidth">
        <el-input v-model="addForm.nursingRequirement" autocomplete="off" />
      </el-form-item>
      <el-form-item label="备注" :label-width="formLabelWidth">
        <el-input v-model="addForm.remark" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save()">
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>


  <el-button type="primary" @click="addProject()">添加护理等级</el-button>
   <el-form :inline="true" :model="searchForm" class="demo-form-inline">
    <el-form-item label="护理等级名称">
      <el-input v-model="searchForm.name" placeholder="请输入护理等级名称" clearable />
    </el-form-item>
    <el-form-item label="护理等级状态">
      <el-select
        v-model="searchForm.status"
        placeholder="请选择护理等级状态"
        clearable
        style="width: 180px"
      >
        <el-option label="正常" value="1" />
        <el-option label="异常" value="0" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="getLevel">查询</el-button>
    </el-form-item>
    </el-form>
  <el-table :data="tableData" border style="width: 100%">
    <el-table-column type="index" label="序号" />
    <el-table-column prop="id" label="编号" />
    <el-table-column prop="name" label="护理等级名称" />
    <el-table-column prop="lplanId" label="护理等级计划ID" />
    <el-table-column prop="fee" label="护理费用" />
    <el-table-column prop="description" label="等级说明" />
    <el-table-column prop="status" label="护理状态" />
    <el-table-column prop="createTime" label="创建时间" />
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" size="mini" @click="getById(scope.row.id)">编辑</el-button>
        <el-button type="danger" size="mini" @click="deleteById(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
   <el-pagination
      :current-page="currentPage"
      :page-size="pageSize"
      :page-sizes="[5, 10, 15, 20]"
      :size="size"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
</template>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>